# -*- coding:UTF-8 -*-

import mysql
import pandas as pd






if __name__ == "__main__":


    fund_arr = [{'Code': 'sh000300'}]

    for a in mysql.getDiyFunds("select * from fund_tmp"):
        fund_arr.append(a)

    fundNames = ['交易日期']

    data_arr = []
    # print(fund_arr)


    for fundId in fund_arr:

        sql = "select fundName from fund where fundId='%s'" % (fundId['Code'])
        # print(sql)

        records = mysql.getDataBySQL(sql)

        if len(records) == 0:
            fundName = "沪深300"
            print("转化" + fundName)
            fundNames.append(fundName)
        else:
            fundName = records[0][0]
            print("转化" + fundName)
            fundNames.append(records[0][0])

        sql =  "select FSRQ, IF(JZZZL IS NULL, 0, JZZZL) 'JZZZL' from jjjz where fundId = '%s' ORDER BY FSRQ DESC limit 30" % (fundId['Code'])
        # print(sql)

        records = mysql.getDataBySQL(sql)

        if fundId['Code'] == "sh000300":
            data_arr = records
        else:
            count = len(records)
            i = 0
            # print(data_arr)
            while i < count:
                # print(records[i][1])
                # print(data_arr[i])
                data_arr[i].append(records[i][1])
                i += 1


        # print(data_arr)


    df = pd.DataFrame(data_arr, columns=fundNames)

    df.to_csv("../投资数据/基金涨跌幅对比.csv",index=False,sep=',')

